/**
 * 
 */
package com.youth.fund.hk.hostManager.manager.dao.mybatis;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.youth.fund.hk.hostManager.common.Constants;
import com.youth.fund.hk.hostManager.common.dao.mybatis.BaseMybatisDao;
import com.youth.fund.hk.hostManager.manager.dao.CaseDao;
import com.youth.fund.hk.hostManager.manager.datamodel.CaseModel;

/**
 * @author Sunk_Sky
 *
 * 2014年11月26日下午8:39:28
 */
@Repository("CaseDao")
public class CaseMybatisDaoImpl extends BaseMybatisDao<CaseModel, Integer> implements CaseDao
{
	private final static String CREATE_ZH_CN = "case_zh_cn.create";

	private final static String DELETE_ZH_CN = "case_zh_cn.delete";

	private final static String UPDATE_ZH_CN = "case_zh_cn.update";

	private final static String QUERY_BY_CONDITION_ZH_CN = "case_zh_cn.list_by_condition";
	
	private final static String CREATE_ZH_HK = "case_zh_hk.create";

	private final static String DELETE_ZH_HK = "case_zh_hk.delete";

	private final static String UPDATE_ZH_HK = "case_zh_hk.update";

	private final static String QUERY_BY_CONDITION_ZH_HK = "case_zh_hk.list_by_condition";
	
	private final static String CREATE_EN = "case_en.create";

	private final static String DELETE_EN = "case_en.delete";

	private final static String UPDATE_EN = "case_en.update";

	private final static String QUERY_BY_CONDITION_EN = "case_en.list_by_condition";
	/* (non-Javadoc)
	 * @see com.youth.fund.hk.hostManager.common.dao.ICommonDao#create(java.io.Serializable)
	 */
	@Override
	public boolean create(CaseModel model)
	{
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());

		model.setSetup_time(timestamp);

		model.setUpdate_time(timestamp);
		
		if(Constants.LANG_EN.equalsIgnoreCase(model.getLang()))
		{
			return super.create(model, CREATE_EN);
		}else if(Constants.LANG_ZH_CN.equalsIgnoreCase(model.getLang()))
		{
			return super.create(model, CREATE_ZH_CN);
		}else
		{
			return super.create(model, CREATE_ZH_HK);
		}
	}

	/* (non-Javadoc)
	 * @see com.youth.fund.hk.hostManager.common.dao.ICommonDao#delete(java.io.Serializable)
	 */
	@Override
	public boolean delete(CaseModel model)
	{
		if(Constants.LANG_EN.equalsIgnoreCase(model.getLang()))
		{
			return super.delete(model, DELETE_EN);
		}else if(Constants.LANG_ZH_CN.equalsIgnoreCase(model.getLang()))
		{
			return super.delete(model, DELETE_ZH_CN);
		}else
		{
			return super.delete(model, DELETE_ZH_HK);
		}
	}

	/* (non-Javadoc)
	 * @see com.youth.fund.hk.hostManager.common.dao.ICommonDao#update(java.io.Serializable)
	 */
	@Override
	public boolean update(CaseModel model)
	{
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());

		model.setUpdate_time(timestamp);
		
		if(Constants.LANG_EN.equalsIgnoreCase(model.getLang()))
		{
			if(checkExist(model))
			{
				return super.update(model, UPDATE_EN);
			}
			return this.create(model);
		}else if(Constants.LANG_ZH_CN.equalsIgnoreCase(model.getLang()))
		{
			if(checkExist(model))
			{
				return super.update(model, UPDATE_ZH_CN);
			}
			return this.create(model);
		}else
		{
			if(checkExist(model))
			{
				return super.update(model, UPDATE_ZH_HK);
			}
			return this.create(model);
		}
	}
	
	private boolean checkExist(CaseModel model)
	{
		boolean result = false;
		
		CaseModel condition = new CaseModel();
		
		condition.setId(model.getId());
		
		condition.setType_id(model.getType_id());
		
		CaseModel temp = this.queryOne(condition);
		
		if(temp != null&& temp.getType_id()!=-1)
		{
			result = true;
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.youth.fund.hk.hostManager.common.dao.ICommonDao#query(java.io.Serializable)
	 */
	@Override
	public List<CaseModel> query(CaseModel model)
	{
		if(Constants.LANG_EN.equalsIgnoreCase(model.getLang()))
		{
			return super.query(model, QUERY_BY_CONDITION_EN);
		}else if(Constants.LANG_ZH_CN.equalsIgnoreCase(model.getLang()))
		{
			return super.query(model, QUERY_BY_CONDITION_ZH_CN);
		}else
		{
			return super.query(model, QUERY_BY_CONDITION_ZH_HK);
		}
	}

	/* (non-Javadoc)
	 * @see com.youth.fund.hk.hostManager.common.dao.ICommonDao#queryOne(java.io.Serializable)
	 */
	@Override
	public CaseModel queryOne(CaseModel model)
	{
		CaseModel result ;
		
		if(Constants.LANG_EN.equalsIgnoreCase(model.getLang()))
		{
			result = super.queryOne(model, QUERY_BY_CONDITION_EN);
		}else if(Constants.LANG_ZH_CN.equalsIgnoreCase(model.getLang()))
		{
			result = super.queryOne(model, QUERY_BY_CONDITION_ZH_CN);
		}else
		{
			result = super.queryOne(model, QUERY_BY_CONDITION_ZH_HK);
		}
		
		if(result == null)
		{
			result =  new CaseModel();
		}
		return result;
		
	}
	
}
